package com.hok.misc.http;

import java.io.IOException;
import java.io.InputStream;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.auth.Credentials;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.DefaultProxyAuthenticationHandler;


public class Test {
	
	public static void main(String[] args) throws ClientProtocolException, IOException, InterruptedException {
		for (int i = 0; i < 5; i++) {
			Runtime.getRuntime().exec("C:\\Program Files\\Internet Explorer\\iexplore.exe http://bbs.asiainfo-linkage.com/bbs/showtopic-2258.aspx");
			Thread.sleep(15000);			
		}
//		for (int j = 0; j < 2000; j++) {
//			Runtime.getRuntime().exec("taskkill /IM iexplore.exe");
//		}
	}
	
	public static void proxyHttpClient() throws ClientProtocolException, IOException {
		DefaultHttpClient httpclient = new DefaultHttpClient();
		HttpHost proxy = new HttpHost("hzproxy.asiainfo-linkage.com", 8080);
		httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
		
		HttpGet httpget = new HttpGet("http://bbs.asiainfo-linkage.com/bbs/showtopic-2258.aspx");
		Credentials defaultcreds = new UsernamePasswordCredentials("zhanght3", "hiHI58%*");
		DefaultProxyAuthenticationHandler proxyAuthHandler = new DefaultProxyAuthenticationHandler();
		
		httpclient.setProxyAuthenticationHandler(proxyAuthHandler);
//		httpclient.setProxyCredentials(new AuthScope("hzproxy.asiainfo-linkage.com", 8080, AuthScope.ANY_REALM), defaultcreds);
		HttpResponse response = httpclient.execute(httpget);
		HttpEntity entity = response.getEntity();
		if (entity != null) {
		    InputStream instream = entity.getContent();
		    int l;
		    byte[] tmp = new byte[2048];
		    while ((l = instream.read(tmp)) != -1) {
		    	System.out.println(new String(tmp,"utf-8"));
		    }
		}
	}
	
	public static void basicHttpClient() throws ClientProtocolException, IOException {
		HttpClient httpclient = new DefaultHttpClient();
		HttpGet httpget = new HttpGet("http://bbs.asiainfo-linkage.com/bbs/showtopic-2258.aspx");
		HttpResponse response = httpclient.execute(httpget);
		HttpEntity entity = response.getEntity();
		if (entity != null) {
		    InputStream instream = entity.getContent();
		    int l ;
		    byte[] tmp = new byte[2048];
		    while ((l = instream.read(tmp)) != -1) {
		    	System.out.println(new String(tmp,"gb2312"));
		    }
		}
	}
}
